var params = {};

var userPermission="用户权限";
var userGroupPermission="用户组权限";
var rolePermission="角色权限";

var current = {};

var option = {};

$(function(){
	$("ul.tabtit").on("click",changeTab);

	loadCurrentPage();
});

function loadCurrentPage(){
	
	var target = "ul.tabtit .current";
	
	if($(target).text()==userPermission){
    	current.name = userPermission;
    	setUserPermissionOption();
    }else if($(target).text()==userGroupPermission){
    	current.name = userGroupPermission;
    	setUserGroupPermissionOption();
    }else if($(target).text()==rolePermission){
    	current.name = rolePermission;
    	setRolePermissionOption();
    }

	loadAllData(option);
}

function changeTab(event){
	
	var target = event.target;
	
    if($(target).text()==userPermission){
    	current.name = userPermission;
    	setUserPermissionOption();
    }else if($(target).text()==userGroupPermission){
    	current.name = userGroupPermission;
    	setUserGroupPermissionOption();
    }else if($(target).text()==rolePermission){
    	current.name = rolePermission;
    	setRolePermissionOption();
    }

	loadAllData(option);
}

function loadAllData(option){
	$('#'+option.target).empty();
	params.currentPage = 1;
	params.pageSize = 0;
	params.status='Y';
	$.ajax({
		url:option.url,
		type:'POST',
		data:params
	}).done(function(data){
		var result = data.bo.list;
		for(var i=0;i<result.length;i++)
		{
			var name='',icon='';
			if(option.type=='user')
			{
				name = result[i].username;
				icon = 'fa fa-user';
			}else if(option.type=='group'){
				name = result[i].groupname;
				icon = 'fa fa-users';
			}else if(option.type=='role'){
				name = result[i].rolename;
				icon = 'fa fa-user-secret';
			}
			$('#'+option.target).append('<div id="'+result[i].id+'"><i class="'+icon+'"></i>'+name+'</div>');
		}
		$('#'+option.target+' div').on('click',function(){
			$('#'+option.target+' div').removeClass('selected');
			$(this).addClass('selected');
			var id = $(this).attr('id');
			option.id = id;
			loadAllMenu(option);
			loadAllModule(option);
		});
		$('#'+option.target+' div').eq(0).click();
	});
}

function loadAllMenu(option){
	var params = {};
	params.id = option.id;
	params.type = option.type;
	$.ajax({
		url: webUrl+'sysPermission/getMenuPermission',
		type:'POST',
		data:params
	}).done(function(data){
		var treedata = data.bo;
		var tree = $('#'+option.type+'MenuTree');
		tree.tree({
			checkbox:true,
			animate:true,
			cascadeCheck:false,
			onCheck:function(node){
				updatePermission(node,option,'Menu');
	        },
	        formatter:function(node){
	        	
	        	if(node.checked==0)
	        	{
	        		node.checked = false;
	        	}else if(node.checked==1){
	        		node.checked = true;
	        	}
	        	
	        	node.text=node.name;
	        	return node.text;
	        },
		    data:treedata
		});
	});
}

function loadAllModule(option){
	var params = {};
	params.id = option.id;
	params.type = option.type;
	$.ajax({
		url: webUrl+'sysPermission/getFuncPermission',
		type:'POST',
		data:params
	}).done(function(data){
		var treedata = data.bo;
		var tree = $('#'+option.type+'ModuleTree');
		tree.tree({
			checkbox:true,
			cascadeCheck:false,
			onlyLeafCheck:true,
			animate:true,
			onCheck:function(node){
				updatePermission(node,option,'Module');
	        },
	        formatter:function(node){
	        	if(node.checked==0)
	        	{
	        		node.checked = false;
	        	}else if(node.checked==1){
	        		node.checked = true;
	        	}
	        	
	        	node.text = node.name;
	    		return node.text;
	    	},
		    data:treedata
		});
	});
}

function updatePermission(node,option,tree){
	var params = {};
	params.type = option.type;
	if(node.checked==false)
	{
		params.flag = 'N';
	}else{
		params.flag = 'Y';
	}
	
	if(option.type=='user'){
		params.id = $('#allUser .selected').attr('id');
	}else if(option.type=='group'){
		params.id = $('#allGroup .selected').attr('id');
	}else if(option.type=='role'){
		params.id = $('#allRole .selected').attr('id');
	}
	params.permissionid = node.permissionid;

	$.ajax({
		url: webUrl+'sysPermission/saveOrUpdatePermission',
		type:'POST',
		data:params
	});
	
	if(tree=='Menu')
	{
		var menuTree = $('#'+option.type+'MenuTree');
		//父菜单取消，则取消勾选的子菜单
		var childrens = menuTree.tree('getChildren',node.target);
		if(node.checked==false && node.level==1 && childrens.length>0){
			for(var i in childrens)
			{
				if(childrens[i].checked==true)
				{
					menuTree.tree('uncheck', childrens[i].target);
				}
			}
		}else if(node.checked==true && node.level==2){//选择则菜单时，若父菜单未勾选，则自动勾选父菜单
			var parent = menuTree.tree('getParent',node.target);
			if(parent.checked==false)
			{
				menuTree.tree('check', parent.target);
			}
		}
	}
}

function searchFunc(obj){
	var parents = $("[id*='ModuleTree'] > li > div.tree-node");
	var modulName = $(obj).val();
	var i = 0;flag = false;
	parents.each(function(index){
		var module = $(parents[index]).text();
		if(module.indexOf(modulName)>-1)
		{
			var moduleTree = $('#userModuleTree');
			moduleTree.tree('select', parents[index]);
			i=index;
			flag = true;
		}
		if(flag){
			return;
		}
	});
}

function onUserInput(obj){
	params.username = $(obj).val();
	setUserPermissionOption();
	loadAllData(option);
}

function onGroupInput(obj){
	params.groupname = $(obj).val();
	setUserGroupPermissionOption();
	loadAllData(option);
}

function onRoleInput(obj){
	params.rolename = $(obj).val();
	setRolePermissionOption();
	loadAllData(option);
}

function setUserPermissionOption(){
	option = {};
	option.url = webUrl+'sysUser/pageSysUser';
	option.target = 'allUser';
	option.type = "user";
}

function setUserGroupPermissionOption(){
	option = {};
	option.url = webUrl+'sysGroup/pageSysUserGroup';
	option.target = 'allGroup';
	option.type = "group";
}

function setRolePermissionOption(){
	option = {};
	option.url = webUrl+'sysRole/pageSysRole';
	option.target = 'allRole';
	option.type = "role";
}